//
// Created by 郝羽 on 2018/1/10.
//

#ifndef RTC_XSWITCH2_LOGGER_HPP
#define RTC_XSWITCH2_LOGGER_HPP

#include <chrono>
#include <iostream>

class Log {
public:
    enum class Level {
        None,
        Error,
        Info,
        Debug,
    };

    static std::ostream& Error() {
        std::clog.clear(Level::Error <= displayLevel_ ? std::ios_base::goodbit : std::ios_base::badbit);
        return prefix("ERROR");
    }

    static std::ostream& Info() {
        std::clog.clear(Level::Info <= displayLevel_ ? std::ios_base::goodbit : std::ios_base::badbit);
        return prefix("INFO");
    }

    static std::ostream& Debug() {
        std::clog.clear(Level::Debug <= displayLevel_ ? std::ios_base::goodbit : std::ios_base::badbit);
        return prefix("DEBUG");
    }

private:
    static std::ostream& prefix(const std::string& level) {
        auto now = std::chrono::system_clock::now();
        auto now_c = std::chrono::system_clock::to_time_t(now);
        char now_str[100];
        strftime(now_str, sizeof(now_str), "%Y/%m/%d %T", std::localtime(&now_c));
        std::clog << std::string(now_str);
        std::clog << " " << level << " ";
        return std::clog;
    }

    const static Level displayLevel_ = Level::Info;
};

#endif //RTC_XSWITCH2_LOGGER_HPP
